package com.my.controller;

import com.my.anno.Log;
import com.my.entity.Emp;
import com.my.entity.EmpQueryParam;
import com.my.entity.PageBean;
import com.my.entity.Result;
import com.my.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
public class EmpController {
    @Autowired
    private EmpService empService;

    /** 分页查询
     // 简单分页查询
     @GetMapping("/emps") public Result page(@RequestParam(defaultValue = "1") Integer page,
     @RequestParam(defaultValue = "10") Integer pageSize) {
     log.info("分页查询:{},{}", page, pageSize);
     PageBean pageBean = empService.page(page,pageSize);
     return Result.success(pageBean);
     }
     */

    /** 条件查询 方式一：多个参数接收
     @GetMapping("/emps") public Result page(String name, Integer gender,
     @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
     @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end,
     @RequestParam(defaultValue = "1") Integer page,
     @RequestParam(defaultValue = "10") Integer pageSize) {
     log.info("分页查询:{},{},{},{},{},{}", name, gender, begin, end, page, pageSize);
     // PageBean pageBean = empService.page(name, gender, begin, end, page, pageSize);
     return Result.success();
     }*/

    /**
     * 条件查询 方式二: 包装类
     */
    @GetMapping("/emps")
    public Result page(EmpQueryParam backage) {
        log.info("分页查询:{},{},{},{},{},{}",
                backage.getName(), backage.getGender(), backage.getBegin(), backage.getEnd(), backage.getPage(), backage.getPageSize());
        PageBean pageBean = empService.page(backage);
        return Result.success(pageBean);
    }

    /**
     * 新增员工
     *
     * @param emp
     * @return
     */
    @Log
    @PostMapping("/emps")
    public Result save(@RequestBody Emp emp) throws Exception {
        log.info("新增员工：{}", emp);
        empService.save(emp);
        return Result.success();
    }

    @Log
    @DeleteMapping("/emps")
//    public Result delete(Integer[] ids) throws Exception {
    // 从前端接收数据是数组对象时需要加注解 @RequestParam
    public Result delete(@RequestParam List<Integer> ids) {
        log.info("删除员工的id{}", ids);
        empService.delete(ids);
        return Result.success();
    }

    /**
     * 员工回显
     *
     * @param id
     * @return
     */
    @GetMapping("/emps/{id}")
    public Result getById(@PathVariable Integer id) {
        log.info("回显员工的id = {}", id);
        Emp emp = empService.getById(id);
        return Result.success(emp);
    }

    @Log
    @PutMapping("/emps")
    public Result update(@RequestBody Emp emp) {
        log.info("修改的员工信息:{}", emp);

        empService.update(emp);
        return Result.success();
    }
}
